<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'quantity', validatorRules.quantity]" placeholder="请输入数量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'mcode', validatorRules.mcode]" placeholder="请输入产品编码"></p-input>
        </p-form-item>
        <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入产品名称"></p-input>
        </p-form-item>
        <p-form-item label="工站编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'workstationCode', validatorRules.workstationCode]" placeholder="请输入工站编码"></p-input>
        </p-form-item>
        <p-form-item label="工站名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'workstationName', validatorRules.workstationName]" placeholder="请输入工站名称"></p-input>
        </p-form-item>
        <p-form-item label="产品描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'sprc', validatorRules.sprc]" placeholder="请输入产品描述"></p-input>
        </p-form-item>
        <p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'mdescription', validatorRules.mdescription]" placeholder="请输入物料描述"></p-input>
        </p-form-item>
        <p-form-item label="工站所需数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'workstationQuantity', validatorRules.workstationQuantity]" placeholder="请输入工站所需数量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="是否关键物料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'theKeyMaterials', validatorRules.theKeyMaterials]" placeholder="请输入是否关键物料" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'mexplain', validatorRules.mexplain]" placeholder="请输入物料描述"></p-input>
        </p-form-item>
        <p-form-item label=" 产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'productcode', validatorRules.productcode]" placeholder="请输入 产品编码"></p-input>
        </p-form-item>
        <p-form-item label=" 产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'productname', validatorRules.productname]" placeholder="请输入 产品名称"></p-input>
        </p-form-item>
        <p-form-item label="产品规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'productsprc', validatorRules.productsprc]" placeholder="请输入产品规格"></p-input>
        </p-form-item>
        <p-form-item label="是否关键物料 " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['gjwl', validatorRules.gjwl]" :trigger-change="true" dictCode="ba_yn" placeholder="请选择是否关键物料 "/>
        </p-form-item>
        <p-form-item label="SAP管控类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['sap', validatorRules.sap]" :trigger-change="true" dictCode="sap" placeholder="请选择SAP管控类型"/>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MesPeBomMaterialDistributionModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          quantity: {rules: [
          ]},
          mcode: {rules: [
            {required: true, message: '请输入产品编码!'},
          ]},
          mname: {rules: [
            {required: true, message: '请输入产品名称!'},
          ]},
          workstationCode: {rules: [
            {required: true, message: '请输入工站编码!'},
          ]},
          workstationName: {rules: [
          ]},
          sprc: {rules: [
            {required: true, message: '请输入产品描述!'},
          ]},
          mdescription: {rules: [
          ]},
          workstationQuantity: {rules: [
          ]},
          theKeyMaterials: {rules: [
          ]},
          mexplain: {rules: [
          ]},
          productcode: {rules: [
            {required: true, message: '请输入 产品编码!'},
          ]},
          productname: {rules: [
            {required: true, message: '请输入 产品名称!'},
          ]},
          productsprc: {rules: [
            {required: true, message: '请输入产品规格!'},
          ]},
          gjwl: {rules: [
          ]},
          sap: {rules: [
          ]},
        },
        url: {
          add: '/mes-craft/auto/mesPeBomMaterialDistribution/add',
          edit: '/mes-craft/auto/mesPeBomMaterialDistribution/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'quantity','mcode','mname','workstationCode','workstationName','sprc','mdescription','workstationQuantity','theKeyMaterials','mexplain','productcode','productname','productsprc','gjwl','sap'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'quantity','mcode','mname','workstationCode','workstationName','sprc','mdescription','workstationQuantity','theKeyMaterials','mexplain','productcode','productname','productsprc','gjwl','sap'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>